
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/user_member_request.dart';
import 'package:elephant_fish/model/user_info_entity.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class FamilyManagerInPage extends StatefulWidget {
  final int id;
  const FamilyManagerInPage({Key key, this.id}) : super(key: key);

  @override
  _FamilyManagerInPageState createState() => _FamilyManagerInPageState();
}

class _FamilyManagerInPageState extends State<FamilyManagerInPage> {

  List<UserInfoEntity> userInfoEntity = [];
  bool isCreator = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Color(0xff140c3d),
      appBar: AppBar(
        backgroundColor: Color(0xff140c3d),
        centerTitle: true,
        leading: GestureDetector(
          child: Icon(
            Icons.arrow_back_ios,
            size: SizeUtil.px(32),
            color: Color(0xfff3f3f3),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
        title: Text(
          '家族管理',
          style: TextStyle(fontSize: SizeUtil.px(32), color: Color(0xfff5f5f5)),
        ),
      ),
      body: Stack(
        children: [
          Container(
            width: size.width,
            height: SizeUtil.px(453),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors:  [
                          Color(0xfffe404e),
                          Color(0xffff2d8f),
                        ]
                      ),
            ),
          ),
          Align(
            alignment: Alignment.topCenter,
            child: Padding(
              padding: EdgeInsets.only(top: SizeUtil.px(136)),
              child: Column(
                children: [
                  Container(
                    width: SizeUtil.px(685),
                    height: SizeUtil.px(463),
                    decoration: BoxDecoration(
                        color: Color(0xff2b244d),
                        borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(16)))
                    ),
                    child: userInfoEntity.length >= 1 ? Stack(
                      children: [
                        Align(
                          alignment: Alignment.topCenter,
                          child: Padding(
                            padding: EdgeInsets.only(top: SizeUtil.px(45)),
                            child: Column(
                              children: [
                                CircleAvatar(
                                  radius: SizeUtil.px(60),
                                  backgroundImage: NetworkImage('http://119.28.131.94:82' + userInfoEntity[0].avatar),
                                ),

                                SizedBox(height: SizeUtil.px(10),),
                                Text(userInfoEntity[0].nickname,style: TextStyle(color: Color(0xfff3f3f3),fontSize: SizeUtil.px(30)),),
                                SizedBox(height: SizeUtil.px(9),),
                                Text('ID:' + userInfoEntity[0].idNumber,style: TextStyle(color: Color(0xffa5a5a5),fontSize: SizeUtil.px(24)),),
                              ],
                            ),
                          ),
                        ),
                        userInfoEntity.length >= 2 ?
                        Align(
                          alignment: Alignment.topLeft,
                          child: Padding(
                            padding: EdgeInsets.only(top: SizeUtil.px(210),left: SizeUtil.px(119)),
                            child: Column(
                              children: [
                                CircleAvatar(
                                  radius: SizeUtil.px(60),
                                  backgroundImage: NetworkImage('http://119.28.131.94:82' + userInfoEntity[1].avatar),
                                ),
                                SizedBox(height: SizeUtil.px(10),),
                                Text(userInfoEntity[1].nickname,style: TextStyle(color: Color(0xfff3f3f3),fontSize: SizeUtil.px(30)),),
                                SizedBox(height: SizeUtil.px(9),),
                                Text('ID:' + userInfoEntity[1].idNumber,style: TextStyle(color: Color(0xffa5a5a5),fontSize: SizeUtil.px(24)),),
                              ],
                            ),
                          ),
                        ) : Container(width: 0,height: 0,),
                        userInfoEntity.length >= 3 ?
                        Align(
                          alignment: Alignment.topRight,
                          child: Padding(
                            padding: EdgeInsets.only(top: SizeUtil.px(210),right: SizeUtil.px(119)),
                            child: Column(
                              children: [
                                CircleAvatar(
                                  radius: SizeUtil.px(60),
                                  backgroundImage: NetworkImage('http://119.28.131.94:82' + userInfoEntity[2].avatar),
                                ),
                                SizedBox(height: SizeUtil.px(10),),
                                Text(userInfoEntity[2].nickname,style: TextStyle(color: Color(0xfff3f3f3),fontSize: SizeUtil.px(30)),),
                                SizedBox(height: SizeUtil.px(9),),
                                Text('ID:' + userInfoEntity[2].idNumber,style: TextStyle(color: Color(0xffa5a5a5),fontSize: SizeUtil.px(24)),),
                              ],
                            ),
                          ),
                        ) : Container(width: 0,height: 0,),

                      ],
                    ) : Container(width: 0,height: 0,),
                  ),
                  SizedBox(height: SizeUtil.px(19),),
                  Container(
                    width: SizeUtil.px(685),
                    height: SizeUtil.px(110),
                    decoration: BoxDecoration(
                        color: Color(0xff2b244d),
                        borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(16)))
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('assets/images/famliy_sum.png',width: SizeUtil.px(59),height: SizeUtil.px(59),),
                        SizedBox(width: SizeUtil.px(18),),
                        Text('每日统计',style: TextStyle(fontSize: SizeUtil.px(29),color: Color(0xfff3f3f3)),),
                        SizedBox(width: SizeUtil.px(27),),
                        Text('2021-02-18至2021-02-24',style: TextStyle(fontSize: SizeUtil.px(21),color: Color(0xffa5a5a5)),),

                      ],
                    ),
                  ),
                ],
              )
            ),
          )
        ],
      ),
    );
  }

  ///加载数据
  Future<void> loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = UserMemeberRequest();
    request.add('id', widget.id).add('type', 2).addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode': '-1'},);
        },
      ));
    }else if(result['code'] == 0){
      List<UserInfoEntity> list = List();
      List<Map<String,dynamic>> listMap = new List<Map<String, dynamic>>.from(result['data']['data']);
      for(int i = 0;i < listMap.length; i++){
        list.add(UserInfoEntity.fromJson(listMap[i]));
      }
      setState(() {
        userInfoEntity = list;
      });

    }
  }
}
